#include "mytextedit.h"

#include <QCloseEvent>
#include <QDir>
#include <QEvent>
#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>

MyTextEdit::MyTextEdit(QWidget *parent, QString filePath, bool isExisted) : QTextEdit(parent)
{
    isSaved = true;
    setCurPath(filePath);
    setWindowTitle(fileName);
    if(isExisted) open(filePath);
    connect(this, &MyTextEdit::textChanged, this, &MyTextEdit::isModified);
}

bool MyTextEdit::save()
{
    QString outputPath = getFilePath();
    QFile file(outputPath);
    if(!file.open(QFile::WriteOnly | QFile::NewOnly)){
        int result = QMessageBox::question(this, "文件已存在", "是否覆盖", QMessageBox::Yes, QMessageBox::No);
        if(result == QMessageBox::No){
            return false;
        }
        file.open(QFile::WriteOnly);
    }
    QTextStream stream(&file);
    stream << this->toPlainText();
    file.close();
    isSaved = true;
    setWindowTitle(fileName);
    return true;
}

bool MyTextEdit::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("另存为"), getCurPath(),
                                                                            tr("Text files (*.txt)"));
    qDebug() << "要保存的文件" << fileName;
    if(!fileName.isEmpty()){
        setCurPath(fileName);
        if(save()){
            return true;
        }
    }
    return false;
}

bool MyTextEdit::open(QString filePath)
{
    QString inputPath = getFilePath();
    QFile file(inputPath);
    if(!file.open(QFile::ReadOnly | QFile::ExistingOnly)){
        QMessageBox::warning(this, "打开出错", "文件不存在！");
        return false;
    }
    QTextStream instream(&file);
    QString text = instream.readAll();
    file.close();
    qDebug() << text;
    setText(text);
    setWindowTitle(fileName);
}

QString MyTextEdit::getFileName()
{
    return this->fileName;
}

void MyTextEdit::setFileName(QString newName)
{
    this->fileName = newName;
    isModified();
}



QString MyTextEdit::getCurPath()
{
    return this->curPath;
}

void MyTextEdit::setCurPath(QString newPath)
{
    QList<QString> splits = newPath.split('/');
    if(splits.length() > 0){
        setFileName(splits[splits.length() - 1]);
        curPath = newPath.left(newPath.length() - splits[splits.length() - 1].length());
    }
}

void MyTextEdit::isModified()
{
    setWindowTitle(getFileName().append("*"));
    isSaved = false;
}

QString MyTextEdit::getFilePath()
{
    QString path;
    // 拼凑路径
    if(curPath.last(1) == '/'){
        path = curPath + fileName;
    }else{
        path = curPath + '/' + fileName;
    }
    return path;
}

void MyTextEdit::closeEvent(QCloseEvent *event)
{
    if(isSaved){
        event->accept();
    }else{
        int result = QMessageBox::information(this, "文件未保存", tr("文件%1尚未保存，是否保存到本地").arg(fileName), QMessageBox::Yes, QMessageBox::No);
        if(result == QMessageBox::Yes){
            if(saveAs()){
                event->accept();
            }else{
                event->ignore();
            }
        }else{
            event->accept();
        }
    }
}

void MyTextEdit::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *menu = new QMenu(this);
    QAction *undo = menu->addAction("撤销(&U)");
    connect(undo, &QAction::triggered, this, &MyTextEdit::undo);
    QAction *redo = menu->addAction("恢复(&R)");
    connect(redo, &QAction::triggered, this, &MyTextEdit::redo);
    menu->addSeparator();
    QAction *cut = menu->addAction("剪切(&T)");
    connect(cut, &QAction::triggered, this, &MyTextEdit::cut);
    QAction *copy = menu->addAction("复制(&C)");
    connect(copy, &QAction::triggered, this, &MyTextEdit::copy);
    QAction *paste = menu->addAction("粘贴(&V)");
    connect(paste, &QAction::triggered, this, &MyTextEdit::paste);
    menu->addSeparator();
    QAction *select = menu->addAction("全选(&A)");
    connect(select, &QAction::triggered, this, &MyTextEdit::selectAll);
    QAction *clear = menu->addAction("清除");
    connect(clear, &QAction::triggered, this, &MyTextEdit::clear);
    menu->exec(event->globalPos());
    delete menu;
}
